<?php

class plans
{
	public $page;
	private $db;

	function __construct($page)
	{
		global $page,$db;
		$this->page =& $page;
		$this->db =& $db;
	}

	function info()
	{
		$plan = new plan(get('id'));
		if (!$plan->exists) return false;
		$this->page->content .= '<plan>';
		$this->page->content .= xml_iterate($plan);
		$this->page->content .= '<schedules>';
		foreach ($plan->schedules as &$schedule) {
			$this->page->content .= xml_iterate($schedule,'schedule');
		}
		$this->page->content .= '</schedules>';
		$this->page->content .= '</plan>';
	}
	
	function insert()
	{
		if (get('new_name')) {
			$plan = new plan();
			$plan->name = get('new_name');
			$plan->description = get('new_description');
			$plan->price = get('new_price');
			$plan->upload = get('new_upload');
			$plan->download = get('new_download');
			$plan->interface = get('new_interface');
			$plan->auth = get('new_auth');
			$plan->insert();
			iptables_refresh();
		} else {
			$this->page->error('{lang_no_name_given}');		
		}
		$this->show_all();
	}

	function update()
	{
		$plan = new plan(get('id'));
		$plan->name = get('new_name');
		$plan->description = get('new_description');
		$plan->price = get('new_price');
		$plan->upload = get('new_upload');
		$plan->download = get('new_download');
		$plan->interface = get('new_interface');
		$plan->auth = get('new_auth');
		$plan->update();
		iptables_refresh();
		$this->show_all();

		return true;
	}

	function delete()
	{
		$plan = new plan(get('id'));
		$plan->delete();
		iptables_refresh();
		$this->show_all();

		return true;
	}
	
	function insert_schedule()
	{
		$plan = new plan(get('id'));
		$plan->insert_schedule(get('day'),get('start'),get('end'),get('upload'),get('download'));
		iptables_refresh();
		$this->info();
		return true;
	}
	
	function remove_schedule()
	{
		$plan = new plan(get('id'));
		$plan->remove_schedule(get('schedule_id'));
		iptables_refresh();
		$this->info();
		return true;
	}

	function show_all()
	{
		global $lang;

		$plans_sql = 'SELECT * FROM `plans`';
		$plans_result = $this->db->query($plans_sql);

		$this->page->content .= '<plans>';
		while ($plan = $plans_result->fetch_object()) {
			$plan->iface = $plan->interface;
			$this->page->content .= '<plan>';
			foreach ($plan as $key => $value) {
				$this->page->content .= '<'.$key.'>' .$value. '</'.$key.'>';
			}
			$this->page->content .= '</plan>';
		}
		$plans_result->free();
		$this->page->content .= '</plans>';
		$this->page->content .= '
			<days>
				<day>
					<value>all</value>
					<label>'.$lang['all'].'</label>
				</day>
				<day>
					<value>workday</value>
					<label>'.$lang['workday'].'</label>
				</day>
				<day>
					<value>weekend</value>
					<label>'.$lang['weekend'].'</label>
				</day>
				<day>
					<value>monday</value>
					<label>'.$lang['monday'].'</label>
				</day>
				<day>
					<value>tuesday</value>
					<label>'.$lang['tuesday'].'</label>
				</day>
				<day>
					<value>wednesday</value>
					<label>'.$lang['wednesday'].'</label>
				</day>
				<day>
					<value>thursday</value>
					<label>'.$lang['thursday'].'</label>
				</day>
				<day>
					<value>friday</value>
					<label>'.$lang['friday'].'</label>
				</day>
				<day>
					<value>saturday</value>
					<label>'.$lang['saturday'].'</label>
				</day>
				<day>
					<value>sunday</value>
					<label>'.$lang['sunday'].'</label>
				</day>
			</days>';	
	}
}



?>